package com.atguigu.gulimall.product;

import lombok.SneakyThrows;


/**
 * 2个线程交替运行
 */
public class ThreadDemoOne{

    public static void main(String[] args) {
         print p = new print();
        new Thread(){
            @SneakyThrows
            public void run(){
                while (true)
                    p.print1();
            }
        }.start();

        new Thread(){
            @SneakyThrows
            public void run(){
                while (true)
                    p.print2();
            }
        }.start();
    }

static class print {

    int flag = 1;

    public void print1() throws InterruptedException {
        synchronized (this) {
            if (flag != 1) {
                this.wait();
            }
            System.out.println("1111");
            flag = 2;
            this.notify();
        }
    }

    public void print2() throws InterruptedException {
        synchronized (this){
            if(flag != 2){
                this.wait();
            }
            System.out.println("2222");
            flag = 1;
            this.notify();
        }

    }
}

}
